package jeeobserver;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Logger;

public class JdbcDriver implements Driver {

	private static final String URL_PREFIX = "jdbc:jeeobserver";

	private Driver realDriver;

	static {
		try {
			JdbcDriver customDriver = new JdbcDriver();

			DriverManager.registerDriver(customDriver);
		} catch (SQLException ex) {
		}
	}

	 @Override
	public Connection connect(String url, Properties info) throws SQLException {

		if (url.startsWith(URL_PREFIX)) {

			String realDriverClass = getRealUrlParameter(url, "driver");

			if (info.getProperty("driver") != null) {
				realDriverClass = info.getProperty("driver");
			}

			if (realDriver != null) {
				try {
					Class.forName(realDriverClass);
				} catch (ClassNotFoundException ex) {
					throw new SQLException("Driver Class " + realDriverClass + " not found.", ex);
				}
			}

			//Load real driver
			this.realDriver = getRealDriver(url);

			//Load real connection
			JdbcConnection connectionObserver = new JdbcConnection(DriverManager.getConnection(getRealUrl(url), info), getRealUrl(url));

			return connectionObserver;
		}

		return null;
	}

	 @Override
	public boolean acceptsURL(String url) throws SQLException {

		if (url.startsWith("jdbc:jeeobserver:")) {
			return true;
		} else {
			return false;
		}
	}

	 @Override
	public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
		if (realDriver != null) {
			return realDriver.getPropertyInfo(url, info);
		}
		return null;
	}

	 @Override
	public int getMajorVersion() {
		if (realDriver != null) {
			return realDriver.getMajorVersion();
		}
		return 1;
	}

	 @Override
	public int getMinorVersion() {
		if (realDriver != null) {
			return realDriver.getMinorVersion();
		}
		return 1;
	}

	 @Override
	public boolean jdbcCompliant() {
		if (realDriver != null) {
			return realDriver.jdbcCompliant();
		}
		return false;
	}

	private String getRealUrl(String url) {
		String realUrl = url.replace(URL_PREFIX, "jdbc");

		if (realUrl.contains("?")) {
			realUrl = realUrl.substring(0, realUrl.indexOf('?'));
		}

		return realUrl;
	}

	private String getRealUrlParameter(String url, String parameter) {
		String realUrl = url.replace(URL_PREFIX, "jdbc");

		if (realUrl.contains("?")) {
			String parameters[] = realUrl.substring(realUrl.indexOf('?') + 1).split("&");

			for (int i = 0; i < parameters.length; i++) {
				String currentParamenter = parameters[i];
				if (currentParamenter.startsWith(parameter + "=")) {
					return currentParamenter.replace(parameter + "=", "");
				}
			}
		}
		return null;
	}

	private Driver getRealDriver(String url) throws SQLException {

		if (url.startsWith(URL_PREFIX)) {

			Enumeration drivers = DriverManager.getDrivers();

			Driver driver;
			while (drivers.hasMoreElements()) {
				driver = (Driver) drivers.nextElement();

				if (driver.acceptsURL(getRealUrl(url))) {
					return driver;
				}
			}
		}
		return null;
	}

	 @Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
